package cj.web.portal.controllers;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.web.admin.constants.Constants;
import cj.web.kly.domain.News;
import cj.web.kly.domain.SubjectColumns;
import cj.web.kly.domain.SubjectNews;
import cj.web.login.controllers.BaseController;
import cj.web.portal.service.SpecialTopicService;

/**
 * 专题
 * @author Administrator
 *
 */
@Path("/specialTopic")
public class SpecialTopicController extends BaseController  {

	private static Logger logger = Logger.getLogger(SpecialTopicController.class.getName());
	
	@Autowired
	private SpecialTopicService specialTopicService;
	
	/**
	 * 栏目页
	 * @return
	 * @throws ParseException
	 */
	@Get("subjectPage")
	public String subjectPage(Model model,@Param("subjectId") int subjectId) throws ParseException {
		Map rep = specialTopicService.querySubjectColumnslis(subjectId);
		model.add("subject", rep.get("subject"));
		model.add("subjectColumnslis", rep.get("subjectColumnslis"));
		return "/views/portal/specialTopic/specialTopic.vm";
	}
	
	/**
	 * 栏目项页
	 * @param model
	 * @param subjectId
	 * @param subjectColumnsId
	 * @return
	 * @throws ParseException
	 */
	@Get("subjectListPage")
	public String subjectListPage(Model model,@Param("subjectId") int subjectId,
			@Param("subjectColumnsId") int subjectColumnsId) throws ParseException {
		Map rep = specialTopicService.querySubjectColumnslis(subjectId,subjectColumnsId);
		model.add("subject", rep.get("subject"));
		model.add("subjectColumns", rep.get("subjectColumns"));
		model.add("subjectColumnslis", rep.get("subjectColumnslis"));
		return "/views/portal/specialTopic/specialTopicList.vm";
	}
	
	/**
	 * 请求方法
	 * @param subjectId
	 * @param start
	 * @param length
	 * @return
	 * @throws ParseException
	 */
	@Post("getSubjectList")
	public String queryList(@Param("subjectColumnsId") int subjectColumnsId,
			@Param("start") int start,@Param("length") int length) throws ParseException {
	
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		Map rep = specialTopicService.query(subjectColumnsId, start*length, length);
		List<News> objs = (List<News>) rep.get("newslis");
		int size = (int) rep.get("size");
		jo.put("list", objs);
		jo.put("totalCount", size);
		return "@"+jo.toString();
	}
	
	/**
	 * 新闻详情
	 * @param model
	 * @param id
	 * @return
	 * @throws ParseException
	 */
	@Get("newsDetailPage")
	public String newsDetailPage(Model model,@Param("subjectColumnsId") int subjectColumnsId,
			@Param("id") int id) throws ParseException {
		List<SubjectNews> newslis = specialTopicService.queryListById(subjectColumnsId, 0, 5);
		//查询栏目信息
		SubjectColumns subjectColumns = specialTopicService.querySubjectColumns(subjectColumnsId);
		model.add("subjectColumns",subjectColumns);
		model.add("newslis",newslis);
		model.add("id",id);
		return "/views/portal/specialTopic/specialTopicDetail.vm";
	}
	
	/**
	 * 新闻详情
	 * @param model
	 * @param id
	 * @return
	 * @throws ParseException
	 */
	@Post("newsDetail")
	public String newsDetail(@Param("id") int id) throws ParseException {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		SubjectNews obj = specialTopicService.querySubjectNewsById(id);
//		List<News> newslis = new ArrayList<News>();
//		newslis.add(obj);
		
		jo.put("obj", obj);
		return "@"+jo.toString();
	}
}